Skip to content

feat: add support for struct simvar marshalling#9

Merged
AussieScorcher merged 4 commits intostopbars:mainfrom
bstudtma:add-get-structure-support
Aug 25, 2025
Merged

feat: add support for struct simvar marshalling#9
AussieScorcher merged 4 commits intostopbars:mainfrom
bstudtma:add-get-structure-support

Conversation

@bstudtma
Copy link
Copy Markdown
Contributor

@bstudtma bstudtma commented Aug 24, 2025

Summary

Added support to GetAsync to retrieve an entire structure at once.

Example:

var snapshot = await client.SimVars.GetAsync<MyApp.AircraftSnapshot>();
Console.WriteLine($"Altitude: {snapshot.AltitudeFeet:F0} ft");
Console.WriteLine($"Airspeed: {snapshot.IndicatedAirspeedKnots:F0} kts");
Console.WriteLine($"Title: {snapshot.Title}");

This will populate a struct like:

[StructLayout(LayoutKind.Sequential)]
public struct AircraftSnapshot
{
    /// <summary>
    /// Gets or sets the pressure altitude of the aircraft in feet.
    /// </summary>
    [SimConnect("PLANE ALTITUDE", "feet", SimConnectDataType.Integer32)]
    public int AltitudeFeet;

    /// <summary>
    /// Gets or sets the indicated airspeed in knots.
    /// </summary>
    [SimConnect("AIRSPEED INDICATED", "knots", SimConnectDataType.FloatDouble)]
    public double IndicatedAirspeedKnots;

    /// <summary>
    /// Gets or sets the aircraft title (name).
    /// </summary>
    [SimConnect("TITLE", null, SimConnectDataType.String260, order: 0)]
    public string Title;
}

Changes Made

  • Added support to GetAsync to return a struct.
  • Added a corresponding test case.

Additional Information

  • May need some polish but seems to be working.

  • order attribute is not required. If set, attributes with an order are sorted first; otherwise, ordering falls back to struct field order.

  • Tried to make attributes optional where possible.

    • Best practice is to set name, unit, and SimConnectDataType explicitly.
    • If any are null, the code attempts to look up the info from the registry.
    • Name is required.
  • Overloads the existing GetAsync method.

  • [StructLayout(LayoutKind.Sequential)] may not be necessary based on late testing.


Author Information

Discord Username: bstudtma


Checklist

[x] Have you followed the guidelines in our Contributing document?
[x] Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Copilot AI review requested due to automatic review settings August 24, 2025 23:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for struct-based SimVar marshalling, allowing developers to retrieve multiple SimVars in a single request by defining structs annotated with SimConnect attributes.

  • Added struct marshalling capability through a new GetAsync<T>() overload that accepts struct types
  • Introduced SimConnectAttribute for annotating struct fields with SimVar metadata
  • Created infrastructure classes for struct binding, field ordering, and data type sizing

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
SimVarManager.cs Added new GetAsync<T>() overload for struct marshalling with reflection-based field binding and data parsing
SimConnectAttribute.cs New attribute class for annotating struct fields with SimVar name, unit, and data type information
SimVarStructBinder.cs Internal utility for validating struct layouts and building SimConnect data definitions from annotated fields
SimVarDataTypeSizing.cs Utility class providing size calculations and offset computations for SimConnect data types
SimVarTests.cs Added test case demonstrating struct marshalling with a Position struct containing latitude, longitude, and altitude

bstudtma and others added 3 commits August 24, 2025 18:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
undoing this suggestion from copilot since it caused the build to fail
@AussieScorcher AussieScorcher merged commit da293cc into stopbars:main Aug 25, 2025
2 checks passed
@bstudtma bstudtma deleted the add-get-structure-support branch August 25, 2025 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants